package com.kzd.btgame.base

import android.app.Activity
import android.app.Application
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.*
import com.kzd.btgame.R
import com.kzd.btgame.app.SPSConstance
import com.kzd.btgame.entity.*
import com.kzd.btgame.jetpack.api.*
import com.kzd.btgame.manage.getDiskCacheDirectoryApk
import com.module.lemlin.http.*
import com.module.lemlin.app.OwnerApplication
import com.module.lemlin.owner.OwnerViewModel
import com.module.lemlin.owner.setRxLifeScope
import com.umeng.socialize.ShareAction
import com.umeng.socialize.UMShareListener
import com.umeng.socialize.bean.SHARE_MEDIA
import com.umeng.socialize.media.UMImage
import com.umeng.socialize.media.UMWeb
import com.zhihu.matisse.Matisse
import com.zhihu.matisse.MimeType
import com.zhihu.matisse.engine.impl.GlideEngine
import com.zhihu.matisse.internal.entity.CaptureStrategy
import rxhttp.wrapper.entity.Progress
import java.lang.StringBuilder

open class BaseViewModel(application: Application) : OwnerViewModel(application) {

    override val httpCodeProPer: Int
        get() = 1

    override fun onTokenOverdue() {
//        SPSConstance.clearUser()
//        toAccessActivity()
    }

    val userAppUpdateLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AppUpdate>>>()
    fun userAppUpDate(delay: Long = 0) {
        setRxLifeScope(SubscribeViewModel(userAppUpdateLiveData)) {
            appIsUpDate(delay)
        }
    }

    val userAppFabLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<HomeFab>>>()
    fun userAppFab() {
        setRxLifeScope(SubscribeViewModel(userAppFabLiveData)) {
            newGetIndexActivity()
        }
    }

    companion object {
        val userAppTipsLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<AppTips>>>()
    }

    fun userAppTips(delay: Long = 0) {
        setRxLifeScope(SubscribeViewModel(userAppTipsLiveData)) {
            newGetTips(delay)
        }
    }

    val userServiceLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserService>>>()
    fun userService() {
        setRxLifeScope(SubscribeViewModel(userServiceLiveData)) {
            newGetServerInfo()
        }
    }

    val userInfoLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserInfo>>>()
    fun userInfo() {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<UserInfo>>(userInfoLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserInfo>) {
                super.onSuccess(resp)
                SPSConstance.userInfo = resp.data
            }
        }) {
            userGetUserInfo()
        }
    }

    val userMineLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserMine>>>()
    fun userMine() {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<UserMine>>(userMineLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserMine>) {
                super.onSuccess(resp)
                SPSConstance.userMine = resp.data
            }
        }) {
            userMyInfo()
        }
    }


    fun userChange(
        face: String? = null, address: String? = null,
        pet_name: String? = null, gender: String? = null,
        qq: String? = null, name: String? = null, idcard: String? = null,
    ) {
        setRxLifeScope(object : SubscribeViewModel<HttpResponseDataT<UserInfo>>(userInfoLiveData) {
            override fun onSuccess(resp: HttpResponseDataT<UserInfo>) {
                super.onSuccess(resp)
                SPSConstance.userInfo = resp.data
            }
        }) {
            userChangeInfo(
                face = face, address = address, pet_name = pet_name, gender = gender,
                qq = qq, name = name, idcard = idcard
            )
        }
    }

    val userCollectAddLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userCollectAdd(gid: String, type: Int) {
        setRxLifeScope(SubscribeViewModel(userCollectAddLiveData)) {
            appCollectAdd(gid = gid, type = type.toString())
        }
    }

    val userCollectDelLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userCollectDel(cid: String, type: Int) {
        setRxLifeScope(SubscribeViewModel(userCollectDelLiveData)) {
            appCollectDel(gid = cid,type = type.toString())
        }
    }

    val userGiftGetLiveData = MutableLiveData<HttpResponseBody<WelfareGiftGetResponse>>()
    fun userGiftGet(lid: String, isDraw: Int, position: Int) {
        setRxLifeScope(object : SubscribeViewModel<WelfareGiftGetResponse>(userGiftGetLiveData) {
            override fun onSuccess(resp: WelfareGiftGetResponse) {
                super.onSuccess(resp.apply {
                    this.isDraw = if (isDraw == 0) 1 else 0
                    this.position = position
                })
            }
        }) {
            appGetGift(lid = lid)
        }
    }

    val userCouponGetLiveData =
        MutableLiveData<HttpResponseBody<HttpResponseDataT<WelfareCoupon>>>()

    fun userCouponGet(cid: String) {
        setRxLifeScope(SubscribeViewModel(userCouponGetLiveData)) {
            userAddCoupon(cid = cid)
        }
    }

    val userUploadLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<String>>>()
    fun userUpload(fileName: String? = null, images: MutableList<String>) {
        setRxLifeScope(SubscribeViewModel(userUploadLiveData)) {
            newUploadFile(fileName, images)
        }
    }

    val userDownloadLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<String>>>()
    fun userDownload(
        urlPath: String, progress: (suspend (Progress) -> Unit)? = null,
    ) {
        setRxLifeScope(SubscribeViewModel(userDownloadLiveData)) {
            val destPath =
                StringBuilder(getDiskCacheDirectoryApk(OwnerApplication.application).path)
                    .append("/").append(AppUtils.getAppName()).append(".apk").toString()
            userDownload(urlPath, destPath, progress)
        }
    }


    val userInviteLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserInvite>>>()
    fun userInvite() {
        setRxLifeScope(SubscribeViewModel(userInviteLiveData)) {
            appInviteFriend()
        }
    }

    val userAgreementLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataT<UserAgree>>>()
    fun userAgreement(type: String) {
        setRxLifeScope(SubscribeViewModel(userAgreementLiveData)) {
            newGetAgreement(type)
        }
    }


    val userAddressLiveData = MutableLiveData<HttpResponseBody<HttpResponseDataE<UserAddress>>>()
    fun userAddress() {
        setRxLifeScope(SubscribeViewModel(userAddressLiveData)) {
            appMyAddress()
        }
    }


    val userAddressKeepLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userAddressKeep(
        id: String? = null, consignee: String, mobile: String, uaddress: String, moren: String,
    ) {
        setRxLifeScope(SubscribeViewModel(userAddressKeepLiveData)) {
            userChangeAddress(id, consignee, mobile, uaddress, moren)
        }
    }


    val userInstallFinishLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userInstallFinish() {
        setRxLifeScope(SubscribeViewModel(userInstallFinishLiveData)) {
            appInstallFinish()
        }
    }

    val userIsShareLiveData = MutableLiveData<Boolean>()
    fun userShare(activity: Activity, url: String, title: String, desc: String) {

        val web = UMWeb(url).apply {
            this.title = title
            this.description = desc
            this.setThumb(UMImage(activity, R.mipmap.logo_gt))
        }

        ShareAction(activity).withMedia(web).setDisplayList(
            SHARE_MEDIA.QQ, SHARE_MEDIA.QZONE,
            SHARE_MEDIA.WEIXIN, SHARE_MEDIA.WEIXIN_CIRCLE
        ).setCallback(object : UMShareListener {
            override fun onStart(p0: SHARE_MEDIA?) {
                userIsShareLiveData.postValue((true))
            }

            override fun onResult(p0: SHARE_MEDIA?) {
                LogUtils.e("onResult=======${p0}=====")
            }

            override fun onError(p0: SHARE_MEDIA?, p1: Throwable?) {
                ToastUtils.showShort(p1?.message)
            }

            override fun onCancel(p0: SHARE_MEDIA?) {
                LogUtils.e("onCancel=======${p0}=====")
            }
        }).open()
    }

    fun userMatisse(activity: FragmentActivity, maxSelectable: Int = 1) {
        Matisse.from(activity)
            .choose(MimeType.ofImage())
            .capture((true))
            .captureStrategy(CaptureStrategy((true), ("com.kzd.btgame.fileProvider")))
            .countable((true))
            .maxSelectable(maxSelectable)
            .imageEngine(GlideEngine())
            .theme(R.style.Matisse_Zhihu1)
            .forResult((100))
    }

    val userDiscussIssueLiveData = MutableLiveData<HttpResponseBody<HttpResponseData>>()
    fun userDiscussIssue(
        id: String,
        content: String,
        reply_uid: String? = null,
        pid: String? = null,
        image: String? = null,
    ) {
        setRxLifeScope(SubscribeViewModel(userDiscussIssueLiveData)) {
            personalAddDiscuss(id, content, reply_uid, pid, image)
        }
    }

}